<template>
  <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="0px"
    :inline-message="true">
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column label="名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.name`" :rules="formRules.name" class="mb-0px!">
            <el-input v-model="row.name" placeholder="请输入名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="目标值" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.targetValue`" :rules="formRules.targetValue" class="mb-0px!">
            <el-input v-model="row.targetValue" placeholder="请输入目标值" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="目标方法" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.targetFunction`" :rules="formRules.targetFunction" class="mb-0px!">
            <el-input v-model="row.targetFunction" placeholder="请输入目标方法" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="责任人" align="center" prop="dutyUserId" min-width="400">
        <template #default="scope">
          <el-select clearable multiple filterable v-model="scope.row.dutyUserId" placeholder="请选择责任人,支持模糊搜索">
            <el-option v-for="dict in userList" :key="dict.id" :label="dict.nickname" :value="dict.id" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加报表消息推送配置</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { ReportViewNewApi } from '@/api/business/reportviewnew'

const props = defineProps<{
  reportId: number // 父表id（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref<any[]>([])
const formRules = reactive({
  reportId: [{ required: true, message: '父表id不能为空', trigger: 'blur' }],
  name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
  targetFunction: [{ required: true, message: '目标方法不能为空', trigger: 'blur' }],
  targetValue: [{ required: true, message: '目标值不能为空', trigger: 'blur' }]


})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.reportId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await ReportViewNewApi.getReportConfigListByReportId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    reportId: undefined,
    name: undefined,
    targetValue: undefined,
    dutyUserId: [],
    dutyUserName: undefined
  }
  row.reportId = props.reportId as any
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}


import * as UserApi from '@/api/system/user'


//用户列表
const userList = ref([])

const initUserList = async () => {
  userList.value = await UserApi.getSimpleUserList()
}

/** 初始化 **/
onMounted(async () => {
  initUserList()
})

defineExpose({ validate, getData })
</script>
